package com.example.springboot002.aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.StringJoiner;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/6/11 19:17
 * @version: v1.0
 */
@Component
@Aspect
public class LogAspect {
    // 功能增强的方法
    @Before("execution(* com.example.springboot002.service..*(..))")
    public void sysLog(JoinPoint joinPoint){
        StringJoiner log = new StringJoiner("|","{","}");
        DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        log.add(dateTimeFormat.format(LocalDateTime.now()));
        String methodName = joinPoint.getSignature().getName();
        log.add(methodName);
        Object[] args = joinPoint.getArgs();
        for (Object arg : args){
            log.add(arg == null ? "-" : arg.toString());
        }
        System.out.println("log = " + log);

    }
}
